package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.R;
import com.example.xianji.bean.LiuYan;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LiuYan_Detail_Adapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<LiuYan> liuyan_list;
//    private SharedPreferences sp;
//    private Editor ed;

    public LiuYan_Detail_Adapter(Context mContext, ArrayList<LiuYan> liuyan_list) {
        super();
        this.mContext = mContext;
        this.liuyan_list = liuyan_list;
//        sp = mContext.getSharedPreferences("text", mContext.MODE_PRIVATE);
//        ed = sp.edit();
    }

    @Override
    public int getCount() {
        return liuyan_list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(mContext, R.layout.liuyan_detail_layout, null);
            mHolder.from_title = (TextView) convertView.findViewById(R.id.from_title);
            mHolder.liuyan_detail = (TextView) convertView.findViewById(R.id.liuyan_detail);
            mHolder.liuyan_time = (TextView) convertView.findViewById(R.id.liuyan_time);
            mHolder.other_from_title = (TextView) convertView.findViewById(R.id.other_from_title);
            mHolder.other_detail = (TextView) convertView.findViewById(R.id.other_detail);
            mHolder.other_time = (TextView) convertView.findViewById(R.id.other_time);
            mHolder.other_nickname = (Button) convertView.findViewById(R.id.other_nickname);
            mHolder.liuyan_nick_name = (Button) convertView.findViewById(R.id.liuyan_nick_name);
            mHolder.linearlayout_me = (LinearLayout) convertView.findViewById(R.id.linearlayout_me);
            mHolder.linearlayout_other = (LinearLayout) convertView.findViewById(R.id.linearlayout_other);
            convertView.setTag(mHolder);
        }

        mHolder = (Holder) convertView.getTag();
        if (liuyan_list.get(position).getAuthor() == PreferenceEngine.getInstance().getInt(Constant.SP.NAME, Constant.SP.KEY_UID, 0)) {
            mHolder.linearlayout_me.setVisibility(View.VISIBLE);
            mHolder.linearlayout_other.setVisibility(View.GONE);
            if (liuyan_list.get(position).getBoothscase().length() > 0) {
                mHolder.from_title.setVisibility(View.VISIBLE);
                mHolder.from_title.setText("来自" + liuyan_list.get(position).getBoothscase() + "的提问");
            } else {
                mHolder.from_title.setVisibility(View.GONE);
            }
            mHolder.liuyan_detail.setText(liuyan_list.get(position).getDetail());
            mHolder.liuyan_time.setText(liuyan_list.get(position).getCreated_at());
            mHolder.liuyan_nick_name.setText("我");
        } else {
            mHolder.linearlayout_me.setVisibility(View.GONE);
            mHolder.linearlayout_other.setVisibility(View.VISIBLE);
            if (liuyan_list.get(position).getBoothscase().length() > 0) {
                mHolder.other_from_title.setVisibility(View.VISIBLE);
                mHolder.other_from_title.setText("来自" + liuyan_list.get(position).getBoothscase() + "的提问");
            } else {
                mHolder.other_from_title.setVisibility(View.GONE);
            }
            mHolder.other_detail.setText(liuyan_list.get(position).getDetail());
            mHolder.other_time.setText(liuyan_list.get(position).getCreated_at());
            mHolder.other_nickname.setText(liuyan_list.get(position).getNickname());
        }
        return convertView;
    }

    class Holder {
        TextView from_title;
        TextView liuyan_detail;
        TextView liuyan_time;
        Button other_nickname;
        Button liuyan_nick_name;
        TextView other_from_title;
        TextView other_detail;
        TextView other_time;
        LinearLayout linearlayout_me;
        LinearLayout linearlayout_other;
    }
}
